#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <mysql/mysql.h>
#include <errno.h>

#include "../inc/agreement.h"
#include "../inc/cJSON.h"
#include "../inc/tcp.h"
#include "../inc/login.h"
#include "../inc/register.h"

static void create_error_response(cJSON *response, const char *type, int code, const char *message);

// 处理客户端发送的JSON指令
int handle_client_cmd(client_info_p client, char *recv_buf, char *resp_buf)
{
    cJSON *root = NULL;
    cJSON *response = NULL;
    const char *type = NULL;

    // 确保缓冲区结尾为NULL
    recv_buf[BUF_SIZE - 1] = '\0';

    printf("收到用户[%s]JSON: %s\n", client->userid, recv_buf);

    // 解析JSON数据
    root = cJSON_Parse(recv_buf);
    if (root == NULL)
    {
        // 解析失败，创建错误响应
        response = cJSON_CreateObject();
        create_error_response(response, "error", -1, "JSON解析失败");

        generate_response_string(response, resp_buf);

        // 释放资源
        cJSON_Delete(response);
        return 0;
    }

    // 获取指令类型
    type = cJSON_GetStringValue(cJSON_GetObjectItem(root, "type"));
    if (type == NULL)
    {
        response = cJSON_CreateObject();
        create_error_response(response, "error", -2, "缺少指令类型(type)");

        generate_response_string(response, resp_buf);

        // 释放资源
        cJSON_Delete(response);
        cJSON_Delete(root);
        return 0;
    }

    // 创建响应对象
    response = cJSON_CreateObject();
    cJSON_AddStringToObject(response, "type", type);

    // 根据指令类型分发处理
    if (strcmp(type, "register") == 0)
    {
        handle_register_cmd(client, root, response);
    }
    else if (strcmp(type, "login") == 0)
    {
        handle_login_cmd(client, root, response);
    }
    else if (strcmp(type, "private_chat") == 0)
    {
        handle_private_chat_cmd(client, root, response);
    }
    else if (strcmp(type, "join_chat_room") == 0)
    {
        handle_join_chat_room_cmd(client, root, response);
    }
    else if (strcmp(type, "chat_room") == 0)
    {
        handle_chat_room_cmd(client, root, response);
    }
    else if (strcmp(type, "get_friendList") == 0)
    {
        handle_get_friendList_cmd(client, root, response);
    }
    else if (strcmp(type, "add_friend") == 0)
    {
        handle_add_friend_cmd(client, root, response);
    }
    else if (strcmp(type, "get_friend_apply_list") == 0)
    {
        handle_get_friend_apply_list_cmd(client, root, response);
    }
    else if (strcmp(type, "friend_apply_action") == 0)
    {
        handle_friend_apply_action_cmd(client, root, response);
    }
    else if (strcmp(type, "log_out") == 0)
    {
        handle_log_out_cmd(client, root, response);
    }
    else if (strcmp(type, "weather") == 0)
    {
        handle_weather_cmd(client);
        return 1;
        // handle_weather_cmd(client, root, response);
    }

    generate_response_string(response, resp_buf);

    // 释放cJSON对象
    cJSON_Delete(response);
    cJSON_Delete(root);

    return 0;
}

// 创建错误响应
static void create_error_response(cJSON *response, const char *type, int code, const char *message)
{
    cJSON_AddStringToObject(response, "type", type);
    cJSON_AddNumberToObject(response, "code", code);
    cJSON_AddStringToObject(response, "message", message);
}

// 整理响应字符串
void generate_response_string(cJSON *response, char *resp_buf)
{
    char *json_str = cJSON_Print(response);
    if (json_str != NULL)
    {
        strncpy(resp_buf, json_str, BUF_SIZE - 1);
        resp_buf[BUF_SIZE - 1] = '\0';
        free(json_str); // 释放cJSON分配的内存
    }
    else
    {
        strncpy(resp_buf, "{\"type\":\"error\",\"code\":-5,\"message\":\"生成响应失败\"}", BUF_SIZE - 1);
        resp_buf[BUF_SIZE - 1] = '\0';
    }
}